home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1776 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.7 KB  |  111 lines

  1. Newsgroups: comp.lang.c++
  2. Path: newsfeed.acns.nwu.edu!ftpbox!mothost!schbbs!news
  3. From: Wendy Weiss x5693 <weiss>
  4. Subject: C access to C++ object data
  5. Content-Type: text/plain
  6. Organization: Motorola GSTG, Scottsdale, AZ
  7. Date: Fri, 12 Jan 1996 20:58:45 GMT
  8. Message-ID: <1996Jan12.205845.22708@schbbs.mot.com>
  9. X-Mailer: Mozilla 1.1 (X11; U; SunOS 4.1C sun4)
  10. Mime-Version: 1.0
  11. X-Url: file:/tmp_mnt/home/pooh/weiss/cplusplus
  12. Content-Transfer-Encoding: 7bit
  13. Sender: news@schbbs.mot.com (SCHBBS News Account)
  14. Nntp-Posting-Host: 192.9.2.17
  15.  
  16. I'm trying to interface between C and C++.  I am able to
  17. call C++ member functions from C (using the extern "C" and nonmember
  18. functions).  Now I want to access object data from C.  I have looked 
  19. at the FAQ:
  20.  
  21. >Can my C function access data in an object of a C++ class?
  22. >
  23. >Sometimes.
  24. >
  25. >You can safely access a C++ object's data from a C function if the C++ class:
  26. >
  27. >   * has no virtual functions (including inherited virtual fns)
  28. >   * has all its data in the same access-level section
  29. >     (private/protected/public)
  30. >   * has no fully-contained subobjects with virtual fns
  31. >
  32. >
  33.  
  34. and I have no problem with all the restrictions.
  35.  
  36.  
  37. Using the example from the C++ faq - I want to access a_ from c-fn.c.
  38. How do I refer to a_ in c-fn.c???  I know that a_ needs to be
  39. public (I changed that from the original example) .
  40. fred->a_ doesn't work, since C doesn't know the structure of the
  41. object.  I know I could write get/put functions, but I want to access
  42. it directly.  Any help would be appreciated.
  43.  
  44.  
  45.  
  46. Here's an example:
  47.  
  48.      /****** C/C++ header file: Fred.h ******/
  49.      #ifdef __cplusplus    /*"__cplusplus" is #defined if/only-if compiler is C++*/
  50.        extern "C" {
  51.      #endif
  52.  
  53.      #ifdef __STDC__
  54.        extern void c_fn(struct Fred*);  /* ANSI-C prototypes */
  55.        extern struct Fred* cplusplus_callback_fn(struct Fred*);
  56.      #else
  57.        extern void c_fn();              /* K&R style */
  58.        extern struct Fred* cplusplus_callback_fn();
  59.      #endif
  60.  
  61.      #ifdef __cplusplus
  62.        }
  63.      #endif
  64.  
  65.      #ifdef __cplusplus
  66.        class Fred {
  67.        public:
  68.          Fred();
  69.          void wilma(int);
  70. /*       private:*/
  71.          int a_;
  72.        };
  73.      #endif
  74.  
  75. "Fred.C" would be a C++ module:
  76.  
  77.      #include "Fred.h"
  78.      Fred::Fred() : a_(0) { }
  79.      void Fred::wilma(int a) : a_(a) { }
  80.  
  81.      Fred* cplusplus_callback_fn(Fred* fred)
  82.      {
  83.        fred->wilma(123);
  84.        return fred;
  85.      }
  86.  
  87. "main.C" would be a C++ module:
  88.  
  89.      #include "Fred.h"
  90.      int main()
  91.      {
  92.        Fred fred;
  93.        c_fn(&fred);
  94.        return 0;
  95.      }
  96.  
  97. "c-fn.c" would be a C module:
  98.  
  99.      #include "Fred.h"
  100.      void c_fn(struct Fred* fred)
  101.      {
  102.        cplusplus_callback_fn(fred);
  103.      }
  104.  
  105.  
  106.  
  107. Thanks,
  108. Wendy Weiss
  109. weiss@pooh.geg.mot.com
  110. weiss@primenet.com
  111.